export default {
    encodeString,
    decodeString
}

function encodeString(string) {
    // return string
    function getNumberString(n, x) {
        n = JSON.stringify(n)
        for (let i = n.length; i < x; i++) {
            n = '0' + n
        }
        return n
    }
    function isLetter(code) {
        if (code >= 65 && code <= 90) {
            return 1
        }
        if (code >= 97 && code <= 122) {
            return 1
        }
        if (code > 90 && code < 97) {
            return 2
        }
        if (code < 65) {
            return 0
        }
        return 2
    }
    string = string.split('')
    let string1 = ''
    for (let i = 0; i < string.length; i++) {
        string1 += JSON.stringify(JSON.stringify(string[i].charCodeAt(0)).length) + JSON.stringify(string[i].charCodeAt(0))
    }
    string1 = string1.split('')
    for (let i = 0; i < string1.length; i++) {
        if (string1[i] == '0') {
            continue
        }
        let c = i
        let ss = string1[c]
        while (true) {
            let cd = isLetter(parseInt(ss))
            if (cd == 2) {
                break
            }
            if (cd == 1) {
                string1[c] = String.fromCharCode(parseInt(ss))
                for (let i1 = i; i1 < c; i1++) {
                    string1[i1] = '-'
                }
                i = c
                break
            }
            c++
            if (c == string1.length) {
                break
            }
            ss += string1[c]
        }
    }
    for (let i = 0; i < string1.length - 1; i++) {
        if (string1[i] == '2' && string1[i + 1] == '2') {
            string1[i] = '?'
            string1[i + 1] = '-'
        }
    }
    let string2 = []
    for (let i = 0; i < string1.length; i++) {
        if (string1[i] == '-') {
            continue
        }
        string2.push(JSON.stringify((string1[i].charCodeAt(0) - string1[i].charCodeAt(0) % 52) / 52) + getNumberString(string1[i].charCodeAt(0) % 52, 2))
    }
    let string3 = ''
    for (let i = string2.length - 1; i >= 0; i--) {
        string3 += string2[i]
    }
    string3 = string3.split('')
    let string4 = []
    for (let i = 0; i < string3.length; i += 2) {
        let ch = string3[i]
        if (i + 1 != string3.length) {
            ch += string3[i + 1]
        }
        if (parseInt(ch) < 52 && ch.length == 2) {
            if (parseInt(ch) < 26) {
                string4.push(String.fromCharCode(parseInt(ch) + 65))
            } else {
                string4.push(String.fromCharCode(parseInt(ch) + 97 - 26))
            }
        } else {
            string4.push(ch)
        }
    }
    let string5 = ''
    for (let i = 0; i < string4.length; i++) {
        string5 += string4[i]
    }
    return string5
}

function decodeString(string) {
    // return string
    function getNumberString(n, x) {
        n = JSON.stringify(n)
        for (let i = n.length; i < x; i++) {
            n = '0' + n
        }
        return n
    }
    string = string.split('')
    for (let i = 0; i < string.length; i++) {
        if (string[i].charCodeAt(0) >= 48 && string[i].charCodeAt(0) <= 56) {
            continue
        }
        if (string[i].charCodeAt(0) <= 90) {
            string[i] = getNumberString(string[i].charCodeAt(0) - 65, 2)
        } else {
            string[i] = getNumberString(string[i].charCodeAt(0) - 97 + 26, 2)
        }
    }
    let string2 = ''
    for (let i = 0; i < string.length; i++) {
        if (string[i] == '-8') {
            string2 += '9'
            continue
        }
        string2 += string[i]
    }
    string2 = string2.split('')
    let string3 = []
    for (let i = 0; i < string2.length; i += 3) {
        string3.push(getNumberString(parseInt(string2[i]) * 52 + parseInt(string2[i + 1] + string2[i + 2]), 3))
    }
    let string4 = []
    for (let i = string3.length - 1; i >= 0; i--) {
        string4.push(String.fromCharCode(parseInt(string3[i])))
    }
    let string5 = ''
    for (let i = 0; i < string4.length; i++) {
        if (string4[i] == '?') {
            string5 += '22'
            continue
        }
        if ((string4[i].charCodeAt(0) >= 48 && string4[i].charCodeAt(0) <= 57) == false) {
            string5 += JSON.stringify(string4[i].charCodeAt(0))
            continue
        }
        string5 += string4[i]
    }
    string5 = string5.split('')
    let string6 = ''
    for (let i = 0; i < string5.length; i++) {
        let n = parseInt(string5[i])
        let s = ''
        for (let i1 = 0; i1 < n; i1++) {
            s += string5[i + 1 + i1]
        }
        string6 += String.fromCharCode(parseInt(s))
        i += n
    }
    return string6
}